<?php
/**
 * Created by PhpStorm.
 * User: liaochungui
 * Email: liaodeity@foxmail.com
 * Date: 2017/11/18
 */

namespace App\Handlers;
class SwooleHandler
{
    public function __construct()
    {

    }

    public function onStart($server, $request)
    {
        echo "server: handshake success with fd{$request->fd}\n";
        //$server->push ($frame, 222);
    }

    public function onMessage($server, $frame)
    {
        $data    = $frame->data;
        $message = json_decode ($data);
        switch ($message->type) {
            case 'login':
                $ret['type']   = 'login';
                $ret['id']   = $message->id;
                $ret['_token'] = md5 ($message->id);

                $server->push ($frame->fd, json_encode ($ret));
                break;
        }

    }

    public function onConnect($server, $frame)
    {
        //var_dump ($frame);
        //$server->push ($frame->fd, 333);
    }

    public function onClose($server, $fd)
    {
        echo "client {$fd} closed\n";
        //$server->push ($frame, 444);
    }

    //private function sendAll($ws, $room_id, $user_id = null, $message = null, $type = 'message')
    //{
    //    foreach ($members as $frame) {
    //        $ws->push ($frame, $message);
    //    }
    //}
}